package test;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    private Socket socket = null;
    public  TcpEchoClient(String serverIp,int serverPort) throws IOException {
        socket = new Socket(serverIp,serverPort);
    }
    public void start(){
        System.out.println("客户端上线！");
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()){
            Scanner scanner = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            Scanner scannerIn  = new Scanner(System.in);
            while(true){
                System.out.print("->");
                String request =  scannerIn.next();
                printWriter.println(request);
                printWriter.flush();
                String response = scanner.next();
                System.out.println(response);
            }
        }catch(IOException e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient t = new TcpEchoClient("127.0.0.1",9090);
        t.start();
    }
}
//1.创建socket 服务器；区别于 DatagramSocket ,socket 不需要创建 IP,Port成员变量，因为内部自带。
                        //DatagramSocket 客户端创建成员变量 是为了 确定服务器